{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "require 'nn'\n",
    "require 'cudnn'\n",
    "require 'inn'\n",
    "require 'image'\n",
    "require 'cuorn'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class_id = {}\n",
    "i = 0\n",
    "local function lines_from(file)\n",
    "-- get all lines from file\n",
    "  if not paths.filep(file) then return {} end\n",
    "  local lines = {}\n",
    "  for line in io.lines(file) do \n",
    "    label = \n",
    "    table.insert(lines,line)\n",
    "    i = i + 1\n",
    "    class_id[string.sub(line,1,9)] = i\n",
    "  end\n",
    "  return lines\n",
    "end\n",
    "\n",
    "classes_name_imagenet = lines_from('synset.txt')\n",
    "\n",
    "-- classes_name_net = lines_from('synset_words.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "function getCAM(img, cam, imw, imh)\n",
    "    cam = 255 * (cam - cam:min()) / (cam:max() - cam:min()) + 1\n",
    "    cam = image.y2jet(cam:float())\n",
    "    cam = image.scale(cam,imw,imh,'bicubic'):mul(0.5)\n",
    "    img = image.scale(img,imw,imh,'bicubic'):mul(0.5)\n",
    "    local img0 = img:float()\n",
    "    img0 = img0:mul(0.5)\n",
    "\n",
    "    cam = cam:add(img0)\n",
    "    \n",
    "    return cam\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cutorch.setDevice(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = torch.load('./SP_GoogLeNet.t7')\n",
    "model = model:cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "imsize = 224\n",
    "image_name = 'ILSVRC2012_val_00000002.JPEG'\n",
    "im = image.load(string.format('/root/Dataset/ILSVRC2012/val_caffe/%s', image_name))\n",
    "-- im = image.load('/root/Util/caffe/examples/images/cat.jpg')\n",
    "\n",
    "print(#im)\n",
    "itorch.image(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "-- preprocess\n",
    "-- local mean_pixel = torch.Tensor({103.939, 116.779, 123.68})\n",
    "-- local perm = torch.LongTensor{3, 2, 1}\n",
    "-- img = img_raw:index(1, perm):mul(256.0)\n",
    "-- mean_pixel = mean_pixel:view(3, 1, 1):expandAs(img)\n",
    "-- img:add(-1, mean_pixel)\n",
    "\n",
    "function ColorNormalize(img, meanstd)\n",
    "    img = img:clone()\n",
    "    for i=1,3 do\n",
    "        img[i]:add(-meanstd.mean[i])\n",
    "        img[i]:div(meanstd.std[i])\n",
    "    end\n",
    "    return img\n",
    "end\n",
    "\n",
    "local meanstd = {\n",
    "   mean = { 0.485, 0.456, 0.406 },\n",
    "   std = { 0.229, 0.224, 0.225 },\n",
    "}\n",
    "img = image.scale(im, imsize, imsize, 'bilinear')\n",
    "img = ColorNormalize(img, meanstd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "input = img:view(1,3,imsize,imsize)\n",
    "scores = model:forward(input:cuda())\n",
    "\n",
    "-- sort scores of class in descending order\n",
    "sc, id = scores:view(-1):sort(true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i=1,5 do\n",
    "    print(id[i],classes_name_imagenet[id[i]], sc[i])\n",
    "end\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fc_weights = model.modules[25].weight\n",
    "print(#fc_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "activations = model.modules[21].output\n",
    "print(#activations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "weights = fc_weights[{532,{}}]\n",
    "print(#weights)\n",
    "weights = weights:view(1,1024,1,1):expandAs(activations)\n",
    "\n",
    "local cam = torch.Tensor({14,14}):fill(1)\n",
    "cam = activations:clone():cmul(weights)\n",
    "cam = cam:sum(2):squeeze()\n",
    "\n",
    "cam = getCAM(im, cam, imsize)\n",
    "\n",
    "itorch.image(cam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "maps = torch.load('cams-sp-gt.t7')\n",
    "clsid = torch.load('./gen/imagenet.t7')\n",
    "clsid = clsid['classList']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "count = 0\n",
    "imsize = 224\n",
    "debug = true\n",
    "\n",
    "if not debug then\n",
    "    fd = io.open('predBox_gt.txt', 'w')\n",
    "end\n",
    "\n",
    "for key, value in pairs(maps) do\n",
    "    count = count + 1    \n",
    "    class_words, image_name = key:match(\"([^,]+)/([^,]+)\")\n",
    "    \n",
    "    img = image.load(string.format('/root/Dataset/ILSVRC2012/val_caffe/%s',image_name))\n",
    "    str = string.format('%d',tonumber(string.sub(image_name,16,23)))\n",
    "    \n",
    "    imw = (#img)[3]\n",
    "    imh = (#img)[2]\n",
    "    \n",
    "    if (#img)[1] == 1 then\n",
    "        tmp = torch.Tensor(3,imh, imw)\n",
    "        tmp[{1,{},{}}] = img\n",
    "        tmp[{2,{},{}}] = img\n",
    "        tmp[{3,{},{}}] = img\n",
    "        img = tmp\n",
    "    end\n",
    "\n",
    "    map = image.scale(value:float(),imw,imh,'bicubic')\n",
    "    map = (map - map:min()) / (map:max() - map:min())\n",
    "    th = map:mean()\n",
    "    a = map:ge(th)\n",
    "    x = a:nonzero()\n",
    "    r = x[{{},1}]\n",
    "    c = x[{{},2}]\n",
    "    \n",
    "    if debug then\n",
    "        print(image_name, classes_name_imagenet[class_id[class_words]])\n",
    "        cam = getCAM(img, map, imsize, imsize)\n",
    "--         itorch.image(cam)\n",
    "        image.save(string.format('./examples/map_%s',image_name), cam)\n",
    "--         print(classes_name_imagenet[class_id[class_words]], c:min(), r:min(), c:max(), r:max())\n",
    "        im_box = image.drawRect(img, c:min(), r:min(), c:max(), r:max(), {lineWidth = 1, color = {0, 255, 0}})\n",
    "--         itorch.image(im_box)\n",
    "        image.save(string.format('./examples/box_%s',image_name), im_box)\n",
    "    else\n",
    "    \n",
    "        x1 = c:min()\n",
    "        y1 = r:min()\n",
    "        x2 = c:max()\n",
    "        y2 = r:max()\n",
    "        res = string.format(' %d %d %d %d %d', class_id[class_words], x1, y1, x2, y2)\n",
    "        str = str..res\n",
    "    end\n",
    "    \n",
    "    if not debug then\n",
    "        fd:write(string.format('%s\\n', str))\n",
    "    end\n",
    "    if debug and count == 500 then\n",
    "        break\n",
    "    end\n",
    "    \n",
    "end\n",
    "\n",
    "if not debug then\n",
    "    fd:close()\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "maps = torch.load('cams-sp-top5.t7')\n",
    "preds = torch.load('pred-sp-top5.t7')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "count = 0\n",
    "imsize = 224\n",
    "debug = true\n",
    "\n",
    "if not debug then\n",
    "    fd = io.open('predBox_topk.txt', 'w')\n",
    "end\n",
    "\n",
    "for key, value in pairs(maps) do\n",
    "    count = count + 1\n",
    "    folder, image_name = key:match(\"([^,]+)/([^,]+)\")\n",
    "    \n",
    "    img = image.load(string.format('/root/Dataset/ILSVRC2012/val_caffe/%s',image_name))\n",
    "    str = string.format('%d',tonumber(string.sub(image_name,16,23)))\n",
    "    \n",
    "    for k=1,5 do      \n",
    "        pr = preds[key]\n",
    "        class_words = clsid[pr[{1,k}]]     \n",
    "        va = value[{k,{}}]:squeeze()\n",
    "        \n",
    "        map = image.scale(va:float(),(#img)[3],(#img)[2],'bicubic')\n",
    "        map = (map - map:min()) / (map:max() - map:min())\n",
    "        th = map:mean()\n",
    "--         th = (map:mean() + map:max())/2\n",
    "        a = map:ge(th)\n",
    "        x = a:nonzero()\n",
    "        r = x[{{},1}]\n",
    "        c = x[{{},2}]\n",
    "        if debug then\n",
    "            print(image_name)\n",
    "--             cam = getCAM(img, value:float(), (#img)[3], (#img)[2])\n",
    "--             itorch.image(cam)\n",
    "            print(classes_name_imagenet[class_id[class_words]], c:min(), r:min(), c:max(), r:max())\n",
    "            im_box = image.drawRect(img, c:min(), r:min(), c:max(), r:max(), {lineWidth = 1, color = {0, 255, 0}})\n",
    "            itorch.image(im_box)\n",
    "\n",
    "        end\n",
    "        x1 = c:min()\n",
    "        y1 = r:min()\n",
    "        x2 = c:max()\n",
    "        y2 = r:max()\n",
    "        res = string.format(' %d %d %d %d %d', class_id[class_words], x1, y1, x2, y2)\n",
    "        str = str..res\n",
    "    end\n",
    "    if not debug then\n",
    "        fd:write(string.format('%s\\n', str))\n",
    "    end\n",
    "    if debug and count == 3 then\n",
    "        break\n",
    "    end\n",
    "end\n",
    "\n",
    "if not debug then\n",
    "    fd:close()\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = torch.Tensor({1,2,3,4,5,6,7})\n",
    "b,c = a:topk(5,1,true,true)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fd:close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = '123'\n",
    "b = '456'\n",
    "c = a+b\n",
    "print(a..b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "iTorch",
   "language": "lua",
   "name": "itorch"
  },
  "language_info": {
   "name": "lua",
   "version": "5.1"
  },
  "ssap_exp_config": {
   "error_alert": "Error Occurs!",
   "initial": [],
   "max_iteration": 4952,
   "recv_id": "u-296bb8e7-ed0e-443c-a15b-5fd312ae",
   "running": [],
   "summary": [],
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
